/*
 * SPAN_INCLUSIVE_INCLUSIVE：前后都包括，在指定范围前后插入新字符，都会应用新样式
 * SPAN_EXCLUSIVE_EXCLUSIVE：前后都不包括，在指定范围前后插入新字符，两端样式无变化
 * SPAN_INCLUSIVE_EXCLUSIVE：前面包括，后面不包括
 * SPAN_EXCLUSIVE_INCLUSIVE：后面包括，前面不包括
 */
package com.fun.library.core;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.fun.library.interfase.IHtmlImageGetter;
import com.fun.library.util.Tools;

/**
 * 作者: Created by AdminFun
 * 邮箱: 614484070@qq.com
 * 创建: 2018/12/24
 * 修改: 2018/12/24
 * 版本: v1.0.0
 * 描述: 自定义ImageGetter，实现HTML显示各种类型的图片。
 * 目前支持 Assets图片、本地图片、项目资源图片(仅支持drawable)、网络图片
 */
public class HtmlGetter implements IHtmlImageGetter {

    private Resources mResources;
    private String mPackName;
    private TextView mTextView;

    public HtmlGetter(@NonNull TextView textView) {
        this.mTextView = textView;
        this.mResources = mTextView.getResources();
        this.mPackName = mTextView.getContext().getPackageName();
    }

    @Override
    public Drawable getDrawable(String source) {
        if (TextUtils.isEmpty(source)) {
            return null;
        }
        Bitmap mBitmap;

        if (source.startsWith(MODEL_ASSETS)) {
            source = source.substring(MODEL_ASSETS.length());
            mBitmap = Tools.getAssetsBitmap(mResources, source);

        } else if (source.startsWith(MODEL_FILE)) {
            source = source.substring(MODEL_FILE.length());
            mBitmap = BitmapFactory.decodeFile(source);

        } else if (source.startsWith(MODEL_DRAWABLE)) {
            source = source.substring(MODEL_DRAWABLE.length());
            mBitmap = Tools.getProjectBitmap(mResources, source, mPackName);

        } else if (source.startsWith(MODEL_HTTP) || source.startsWith(MODEL_HTTPS)) {
            return loadImageFromHttp(source);

        } else {
            mBitmap = null;
        }

        if (mBitmap != null) {
            HtmlDrawable mDrawable = new HtmlDrawable(mResources, mBitmap);
            mDrawable.setBounds(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
            return mDrawable;
        }
        return null;
    }


    /**
     * 获取网络资源图片
     */
    private Drawable loadImageFromHttp(String imageUrl) {
        final LevelListDrawable listDrawable = new LevelListDrawable();
        Glide.with(mTextView).asBitmap().load(imageUrl).into(new SimpleTarget<Bitmap>() {

            @Override
            public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                BitmapDrawable bitmapDrawable = new BitmapDrawable(mResources, bitmap);
                listDrawable.addLevel(1, 1, bitmapDrawable);
                listDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
                listDrawable.setLevel(1);
                mTextView.invalidate();
            }
        });
        return listDrawable;
    }
}